<template>
  <div>
    <el-row>
      <el-col :span="7" style='margin-top:15px;margin-right:10px'>
        <el-card class="box-card">
          <div class="filter-container">
            <el-button-group>
              <el-button type="primary" v-if="menuManager_btn_add" icon="plus" @click="handlerAdd">添加根节点</el-button>
              <!-- <el-button type="primary" v-if="menuManager_btn_edit" icon="edit" @click="handlerEdit" >编辑</el-button> -->
              <el-button type='primary' v-if="menuManager_btn_add" icon="plus" @click="handlerAddSon" :disabled="disabled||!nodeInfo.id">添加子节点
              </el-button>
            </el-button-group>
          </div>
          <el-tree class="filter-tree" :data="treeData" :props="defaultProps" default-expand-all highlight-current node-key="id" :filter-node-method="filterNode" @node-click="getNodeData" ref="treeData">
          </el-tree>
        </el-card>
      </el-col>
      <el-col :span="16" style='margin-top:15px;'>
        <el-card class="box-card">
          <el-form :label-position="labelPosition" :rules="rules" label-width="80px" :model="form" ref="form">
            <h3 v-if="formStatus == 'update'">产品类别编辑</h3>
            <h3 v-if="formStatus == 'create'">节点添加</h3>
            <el-form-item label="父类名称" v-if="form.parentName!=0">
              <span>{{form.parentName}}</span>
            </el-form-item>
            <el-form-item label="排序序号" prop="idx">
              <el-input v-model="form.idx" :disabled="formEdit" placeholder="请输入排序序号"></el-input>
            </el-form-item>
            <el-form-item label="类别编码" prop="code">
              <el-input v-model="form.code" :disabled="formStatus == 'update' || formEdit" placeholder="类别编码"></el-input>
            </el-form-item>
            <el-form-item label="类别名称" prop="nameCn">
              <el-input v-model="form.nameCn" :disabled="formEdit" placeholder="请输入类别名称"></el-input>
            </el-form-item>
            <el-form-item label="英文名称" prop="nameEn">
              <el-input v-model="form.nameEn" :disabled="formEdit" placeholder="请输入英文名称"></el-input>
            </el-form-item>
            <el-form-item label="HS编码" prop="hsCode">
              <el-input v-model="form.hsCode" :disabled="formEdit" placeholder="请输入HS编码"></el-input>
            </el-form-item>
            <el-form-item label="退税率" prop="taxRefundRate">
              <el-input type="number" v-model.number="form.taxRefundRate" :disabled="formEdit" placeholder="请输入退税率">
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
            <el-form-item v-if="formStatus == 'create'">
              <el-button type="primary" @click="create">提交</el-button>
              <el-button @click="onCancel">取消</el-button>
            </el-form-item>
            <el-form-item v-if="formStatus == 'update'">
              <el-button type="primary" @click="update">更新</el-button>
              <el-button @click="onCancel">取消</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { mapGetters } from 'vuex';
import * as api from '@/api/product/category/index';
export default {
  data() {
    return {
      nodeInfo: {},
      rules: {
        idx: {
          required: true,
          message: '请输入排序序号',
          trigger: 'blur'
        },
        code: {
          required: true,
          message: '请输入类别编码',
          trigger: 'blur'
        },
        nameCn: {
          required: true,
          message: '请输入类别名称',
          trigger: 'blur'
        },
        nameEn: {
          required: true,
          message: '请输入英文名称',
          trigger: 'blur'
        },
        hsCode: {
          required: true,
          message: '请输入HS编码',
          trigger: 'blur'
        },
        taxRefundRate: [{
          required: true,
          message: '请输入退税率',
          trigger: 'blur'
        }, {
          type: 'number',
          message: '退税率必须为数字类型'
        }]
      },
      filterText: '',
      list: null,
      total: null,
      formEdit: true,
      formAdd: true,
      formStatus: '',
      showElement: false,
      typeOptions: [],
      listQuery: {
        name: undefined
      },
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      labelPosition: 'right',
      form: {
        parentId: undefined,
        nameCn: undefined,
        code: undefined,
        idx: undefined,
        nameEn: undefined,
        hsCode: undefined,
        taxRefundRate: undefined,
        type: 'authority_menu_menu'
      },
      currentId: -1,
      menuManager_btn_add: false,
      menuManager_btn_edit: false,
      menuManager_btn_eadd: false,
      disabled: false
    }
  },
  watch: {
    filterText(val) {
      this.$refs.treeData.filter(val);
    }
  },
  created() {
    this.menuManager_btn_add = this.elements['menuManager:btn_add'];
    this.menuManager_btn_edit = this.elements['menuManager:btn_edit'];
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  mounted() {
    this.refresh()
  },
  methods: {
    refresh() {
      this.treeData = []
      this.getList();
      this.form = {
        parentId: undefined,
        nameCn: undefined,
        code: undefined,
        idx: undefined,
        nameEn: undefined,
        hsCode: undefined,
        taxRefundRate: undefined,
        type: 'authority_menu_menu'
      }
      this.nodeInfo = {}
      this.formEdit = true
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 获取节点树数据
    getList() {
      api.productTree(this.listQuery).then(response => {
        this.treeData = response.data;
        console.log(response.data)
      });
    },
    getNodeData(val) {
      this.nodeInfo = val
      // debugger
      if (this.form.id) {
        this.formEdit = false;
        this.formStatus = 'update';
      }
      const node = this.$refs.treeData.getCurrentNode()
      if (!this.formEdit) {
        this.formStatus = 'update';
      }
      // 点击某节点获取详情
      api.getDetailById(node.id).then(response => {
        this.form = response.data;
        this.form.parentName = this.nodeInfo.nameCn
      }); let flag = true;
      for (let i = 0; i < this.treeData.length; i++) {
        if (this.treeData[i].id === node.id) {
          flag = false;
        }
        if (this.treeData[i].children && this.treeData[i].children.length) {
          for (let j = 0; j < this.treeData[i].children.length; j++) {
            if (this.treeData[i].children[j].id === node.id) {
              flag = false;
            }
          }
        }
      }
      if (flag) {
        this.disabled = flag
      } else {
        // 不是第三级
        this.disabled = flag
      }
    },
    // 添加根节点
    handlerAdd() {
      this.resetForm();
      this.formEdit = false;
      this.formStatus = 'create';
    },
    // 添加子节点
    handlerAddSon() {
      this.sonForm();
      this.formEdit = false;
      this.formStatus = 'create';
    },
    // 编辑
    update() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        api.updateDetailById(this.form).then(() => {
          this.getList();
          this.$message({
            message: '更新成功',
            type: 'success',
            duration: 2000
          });
        });
      })
    },
    // 提交
    create() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }
        this.form.idx = parseInt(this.form.idx)
        this.form.taxRefundRate = parseInt(this.form.taxRefundRate)
        api.addSingleObj(this.form).then(res => {
          this.getList();
          if (res.code === 200) {
            this.$message({
              message: '创建成功',
              type: 'success'
            });
            return false
          }
          this.$message({
            type: 'error',
            message: res.message
          })
        });
      })
    },
    onCancel() {
      this.formEdit = true;
      this.formStatus = '';
    },
    // 父节点添加传值
    resetForm() {
      this.form = {
        parentId: 0
      };
    },
    // 子节点添加传值
    sonForm() {
      const node = this.$refs.treeData.getCurrentNode()
      console.log(node)
      const parentName = this.nodeInfo.nameCn
      this.form = {
        parentId: node.id,
        cateLevel: ++node.cateLevel,
        parentName
      }
    }
  }
}
</script>
